package com.ruoyi.classai.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.classai.domain.dto.PublicIdQueryInterfaceDto;
import com.ruoyi.classai.domain.vo.homework.HomeWorkAnalysisVo;
import com.ruoyi.classai.domain.vo.homework.HomeWorkSubStudentVo;
import com.ruoyi.classai.domain.vo.homework.HomeWorkVo;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.ruoyi.classai.mapper.EwcHomeworkMapper;
import com.ruoyi.classai.domain.EwcHomework;
import com.ruoyi.classai.service.IEwcHomeworkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;


/**
 * 课后作业Service业务层处理
 * 
 * @author lcj
 * @date 2025-02-28
 */
@Service
public class EwcHomeworkServiceImpl extends ServiceImpl<EwcHomeworkMapper, EwcHomework> implements IEwcHomeworkService {

    @Autowired
    private EwcHomeworkMapper ewcHomeworkMapper;

    @Override
    public AjaxResult add(PublicIdQueryInterfaceDto publicIdQueryInterfaceDto) {
        //查询当前是否已布置作业
        List<EwcHomework> ewcHomeworkList = this.list(Wrappers.<EwcHomework>lambdaQuery()
                .eq(EwcHomework::getModuleResourceUniqueId, publicIdQueryInterfaceDto.getResourceId())
                .eq(EwcHomework::getChapterUniqueId,publicIdQueryInterfaceDto.getChapterUniqueId())
                .eq(EwcHomework::getCourseId,publicIdQueryInterfaceDto.getCourseId())
                .eq(EwcHomework::getClassId,publicIdQueryInterfaceDto.getClassId())
                .eq(EwcHomework::getModuleCode,"04001"));
        if(!ewcHomeworkList.isEmpty()){
            return AjaxResult.error("作业已布置");
        }
        //布置作业
        ewcHomework.setId(UUID.randomUUID().toString().replace("-", ""));
        ewcHomework.setClassId(publicIdQueryInterfaceDto.getClassId());
        ewcHomework.setCourseId(publicIdQueryInterfaceDto.getCourseId());
        ewcHomework.setTeacherId(ThirdSessionHolder.getThirdSession().getId());
        ewcHomework.setChapterUniqueId(publicIdQueryInterfaceDto.getChapterUniqueId());
        ewcHomework.setModuleCode("04001");
        ewcHomework.setModuleResourceUniqueId(publicIdQueryInterfaceDto.getResourceId());
        Date currentDate = new Date();
        ewcHomework.setClassTime(currentDate);
        ewcHomework.setCreateTime(currentDate);
        try {
            // 调用 MyBatis-Plus 提供的插入方法
            boolean result =  save(ewcHomework);
            if(result){
                return AjaxResult.success("提交成功");
            }

        } catch (DuplicateKeyException e) {
            return AjaxResult.error("提交失败");
        }
        return AjaxResult.error("提交失败");
    }

    @Override
    public List<HomeWorkVo> getHomeWorkList(String className, String homeworkType, String deadlineStart, String deadlineEnd) {
        return ewcHomeworkMapper.getHomeWorkList(ThirdSessionHolder.getThirdSession().getId(),className,homeworkType,deadlineStart,deadlineEnd);
    }

    @Override
    public HomeWorkVo getHomeWorkInfo(String homeworkId, String classId) {
        return ewcHomeworkMapper.getHomeWorkInfo(homeworkId,classId);
    }

    @Override
    public List<HomeWorkSubStudentVo> getHomeWorkSubStudentList(String homeworkId, String classId) {
        return ewcHomeworkMapper.getHomeWorkSubStudentList(homeworkId,classId);
    }

    @Override
    public HomeWorkAnalysisVo getHomeWorkAnalysisInfo(String homeworkId, String studentId) {
        return ewcHomeworkMapper.getHomeWorkAnalysisInfo(homeworkId,studentId);
    }

    @Override
    public List<HomeWorkSubStudentVo> getSubStudentHomeWorkList(String homeworkId, String classId, String studentName, String homeworkFlag) {
        return ewcHomeworkMapper.getSubStudentHomeWorkList(homeworkId,classId,studentName,homeworkFlag);
    }
}
